//
//  MVViewController.m
//  花花时尚
//
//  Created by zhang on 15/10/5.
//  Copyright (c) 2015年 ggs. All rights reserved.
//

#import "MVViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "MVTableViewCell.h"
#import "ArtistModel.h"
#import "VideoMode.h"
#import "MVDetailViewController.h"
#import "NightMode.h"

@interface MVViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UITableView *table;

@end

@implementation MVViewController

static NSInteger i = 0;

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}


- (void)startConnect
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *path = [NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/guess.json?D-A=0&offset=0&size=20,&%@", deviceInfo];
    NSLog(@"%@",path);
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"猜你喜欢数据解析成功!");
        NSArray *videoArr = [responseObject objectForKey:@"videos"];
        [self.tableArray addObjectsFromArray:videoArr];
        [self.table reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"猜你喜欢数据解析失败");
    }];
    
    
}

- (void)createTableView{
    CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat height = CGRectGetHeight([UIScreen mainScreen].bounds);
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = 180;
    self.table.backgroundColor = [UIColor clearColor];
//    [self addheader];
    [self addFooter];
    [self.view addSubview:self.table];
    [self.table release];
//    self.table.backgroundColor = [UIColor blackColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";

    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[MVTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    
    NSArray *arr = [self setvalueOfModelWith:indexPath];
    VideoMode *video = [arr firstObject];
    ArtistModel *artist = [arr lastObject];
    [cell setValueWith:video artist:artist];
    cell.layer.cornerRadius = 10;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arr = [self setvalueOfModelWith:indexPath];
    VideoMode *video = [arr firstObject];
    
    MVDetailViewController *detail = [[MVDetailViewController alloc] init];
    detail.id = [video.id integerValue];
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
    
}

- (void)addheader{
    [self.table addHeaderWithCallback:^{
        [self.table reloadData];
        [self.table headerEndRefreshing];
    }];
}

- (void)addFooter{
    [self.table addFooterWithCallback:^{
        i = i + 20;
        NSString *path = [NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/guess.json?D-A=0&offset=%ld&size=20,&%@", (long)i, deviceInfo];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSArray *viedoArr = [responseObject objectForKey:@"videos"];
            [self.tableArray addObjectsFromArray:viedoArr];
            [self.table reloadData];
            [self.table footerEndRefreshing];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }];
}

- (NSArray *)setvalueOfModelWith:(NSIndexPath *)indexPath
{
    NSMutableArray *tempArr = [NSMutableArray array];
    
    NSDictionary *videoDic = [_tableArray objectAtIndex:indexPath.row];
    VideoMode *video = [[VideoMode alloc] init];
    video.descriptions = [videoDic objectForKey:@"description"];
    [video setValuesForKeysWithDictionary:videoDic];
    [tempArr addObject:video];
    
    NSDictionary *artistDic = [[videoDic objectForKey:@"artists"] firstObject];
    ArtistModel *artist = [[ArtistModel alloc] init];
    [artist setValuesForKeysWithDictionary:artistDic];
    [tempArr addObject:artist];
    
    return tempArr;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.view setNeedsDisplay];
    self.navigationController.navigationBar.barTintColor = [NightMode nightModeON]?[UIColor blackColor]:[UIColor whiteColor];
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    [self.table reloadData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self startConnect];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
